//
//  ZHTableViewCell.m
//  宜居HD
//
//  Created by 张金国 on 16/5/3.
//  Copyright © 2016年 张金国. All rights reserved.
//

#import "ZHTableViewCell.h"
#import "Masonry.h"
#import "MAppointmentHousePopViewController.h"
#import "ContinuousLandingViewController.h"
#import "ProjectSigninViewController.h"
#import "PartinActivityViewController.h"
#import "AttentionViewController.h"
#import "UserToAccessViewController.h"


#define kSelfViewBackgroundColor [UIColor colorWithWhite:0.902 alpha:1.000]

@interface ZHTableViewCell ()<UIPopoverControllerDelegate>

@property (nonatomic ,strong)UIImageView  *headImageView;
@property (nonatomic ,strong)UILabel      *nicknameLabel;
@property (nonatomic ,strong)UILabel      *lastimeLabel;
@property (nonatomic ,strong)UILabel      *nameLabel;
@property (nonatomic ,strong)UILabel      *sexLabel;
@property (nonatomic ,strong)UILabel      *ageLabel;
@property (nonatomic ,strong)UILabel      *briefintroductionLabel;
@property (nonatomic ,strong)UIButton     *morebutton;
@property (nonatomic ,strong)UIView       *packView;

@property (nonatomic ,strong)MAppointmentHousePopViewController *mAppointmentHousePopViewController;
@property (nonatomic ,strong)ContinuousLandingViewController    *continuousLandingViewController;
@property (nonatomic ,strong)ProjectSigninViewController        *projectSigninViewController;
@property (nonatomic ,strong)PartinActivityViewController       *partinActivityViewController;
@property (nonatomic ,strong)AttentionViewController            *attentionViewController;
@property (nonatomic ,strong)UserToAccessViewController         *userToAccessViewController;


@property (nonatomic ,strong)UIPopoverController   *popoverController;

@end

@implementation ZHTableViewCell
#pragma mark - init
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

        
        [self setupConstraints];
        [self setupConstraintsForSubviewsAtMoreView];
    }
    return self;
}

#pragma mark - Private
- (void)setupConstraints {
    
    [self.packView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
    
    [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.packView).offset(FiveOffsetViewMargin);
        make.height.equalTo(@40);
        make.width.equalTo(self.headImageView.mas_height);
    }];
 
    [self.nicknameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headImageView.mas_top);
        make.left.equalTo(self.headImageView.mas_right).offset(TenOffsetViewMargin);
        make.height.equalTo(self.headImageView.mas_height).multipliedBy(1/2.0f);
        make.width.equalTo(self.packView.mas_width).multipliedBy(1/10.0f);
    }];

    [self.lastimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headImageView.mas_top);
        make.left.equalTo(self.nicknameLabel.mas_right).offset(TenOffsetViewMargin);
        make.height.equalTo(self.nicknameLabel.mas_height);
        make.width.equalTo(self.packView.mas_width).multipliedBy(1/10.0f);
    }];

    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nicknameLabel.mas_bottom);
        make.left.equalTo(self.nicknameLabel.mas_left);
        make.height.equalTo(self.headImageView.mas_height).multipliedBy(1/2.0f);
        make.width.equalTo(self.packView.mas_width).multipliedBy(1/10.0f);
    }];

    [self.sexLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLabel.mas_top);
        make.left.equalTo(self.nameLabel.mas_right).offset(TenOffsetViewMargin);
        make.height.equalTo(self.nameLabel.mas_height);
        make.width.equalTo(self.nameLabel.mas_width).multipliedBy(1/3.0f);
    }];

    [self.ageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.sexLabel.mas_top);
        make.left.equalTo(self.sexLabel.mas_right).offset(TenOffsetViewMargin);
        make.height.equalTo(self.nameLabel.mas_height);
        make.width.equalTo(self.sexLabel.mas_width);
        
    }];

    [self.briefintroductionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.ageLabel.mas_top);
        make.left.equalTo(self.ageLabel.mas_right).offset(TenOffsetViewMargin);
        make.height.equalTo(self.ageLabel.mas_height);
        make.width.equalTo(self.packView.mas_width).multipliedBy(1/10.0f);
    }];

    [self.morebutton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.headImageView.mas_centerY);
        make.right.equalTo(self.packView.mas_right).offset(-TenOffsetViewMargin);
        make.width.height.equalTo(self.headImageView.mas_height).multipliedBy(2/3.0f);
    }];
    [self.moreView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.headImageView.mas_bottom).offset(FiveOffsetViewMargin);
            make.left.right.equalTo(self.packView);
            make.bottom.equalTo(self.packView.mas_bottom);
            make.height.equalTo(@0).priorityLow();
        }];
 }

#pragma mark - Event response
- (void)moreButtonAction:(UIButton *)button {
    button.selected = !button.selected;
    UITableView *tableView = (UITableView *)self.superview.superview;
    if (button.selected == NO) {
        
        [self.moreView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.headImageView.mas_bottom).offset(FiveOffsetViewMargin);
            make.left.right.equalTo(self.packView);
            make.bottom.equalTo(self.packView);
            make.height.equalTo(@0).priorityLow();
        }];
    }else {
        [self.moreView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@40).priorityLow();
        }];
    }
    
    [tableView reloadData];
    
}

- (void)setupConstraintsForSubviewsAtMoreView {

    NSArray *bgImages = @[@"morebtnBg0",@"morebtnBg1",@"morebtnBg2",@"morebtnBg3",@"morebtnBg4",@"morebtnBg5"];
    UIButton *lastButton = nil;
    for(int i = 0 ; i < 6 ; i++) {
        UIButton  *button  = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [button setImage:[UIImage imageNamed:bgImages[i]] forState:UIControlStateNormal];
        [button.imageView setContentMode:UIViewContentModeScaleAspectFit];
        button.tag = 100 + i;
        [button addTarget:self action:@selector(handleButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
        [self.moreView addSubview:button];
        if (lastButton == nil) {
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.moreView.mas_top).offset(EightOffsetViewMargin);
                make.right.equalTo(self.moreView.mas_right).offset(-TenOffsetViewMargin);
                make.width.equalTo(self.moreView.mas_width).multipliedBy(1/10.0f);
                make.height.equalTo(@25);
            }];
        }else {
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(lastButton.mas_centerY);
                make.width.height.equalTo(lastButton);
                make.right.equalTo(lastButton.mas_left).offset(-TweentyOffsetViewMargin);
            }];
        }
        lastButton = button;
    }
}
#pragma mark - Event response
- (void)handleButtonEvent:(UIButton *)button {
    MoreViewButtnType buttnType = button.tag - 100;
    
    if (buttnType == MoreViewButtnType0) {
        
        [self createPopoverController:self.userToAccessViewController];
        
    } else if (buttnType == MoreViewButtnType1) {
    
        [self createPopoverController:self.attentionViewController];
        
    }else if (buttnType == MoreViewButtnType2) {
        
        [self createPopoverController:self.partinActivityViewController];
        
    }else if (buttnType == MoreViewButtnType3) {
        
        [self createPopoverController:self.projectSigninViewController];
        
    }else if (buttnType == MoreViewButtnType4) {
        
        [self createPopoverController:self.continuousLandingViewController];
        
    }else if (buttnType == MoreViewButtnType5) {
        
        [self createPopoverController:self.mAppointmentHousePopViewController];
    }
    
    [self.popoverController presentPopoverFromRect:button.bounds inView:button permittedArrowDirections:UIPopoverArrowDirectionUp | UIPopoverArrowDirectionDown animated:YES];
    if (self.block != nil) {
        self.block(buttnType);
    }
}

#pragma mark - Getters
- (UIImageView *)headImageView {
    if (!_headImageView) {
        _headImageView = [[UIImageView alloc]init];
        _headImageView.backgroundColor = [UIColor redColor];
        [self.packView addSubview:_headImageView];
    }
    return _headImageView;
}

- (UILabel *)nicknameLabel {
    if (!_nicknameLabel) {
        _nicknameLabel = [[UILabel alloc]init];
        _nicknameLabel.backgroundColor = [UIColor blueColor];
        [self.packView addSubview:_nicknameLabel];
    }
    return _nicknameLabel;
}

- (UILabel *)lastimeLabel {
    if (!_lastimeLabel) {
        _lastimeLabel = [[UILabel alloc]init];
        _lastimeLabel.backgroundColor = [UIColor brownColor];
        [self.packView addSubview:_lastimeLabel];
    }
    return _lastimeLabel;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc]init];
        _nameLabel.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [self.packView addSubview:_nameLabel];
    }
    return _nameLabel;
}
- (UILabel *)sexLabel {
    if (!_sexLabel) {
        _sexLabel = [[UILabel alloc]init];
        _sexLabel.backgroundColor = [UIColor redColor];
        [self.packView addSubview:_sexLabel];
    }
    return _sexLabel;
}

- (UILabel *)ageLabel {
    if (!_ageLabel) {
        _ageLabel = [[UILabel alloc]init];
        _ageLabel.backgroundColor = [UIColor blueColor];
        [self.packView addSubview:_ageLabel];
    }
    return _ageLabel;
}

- (UILabel *)briefintroductionLabel {
    if (!_briefintroductionLabel) {
        _briefintroductionLabel = [[UILabel alloc]init];
        _briefintroductionLabel.backgroundColor = [UIColor redColor];
        [self.packView addSubview:_briefintroductionLabel];
    }
    return _briefintroductionLabel;
}

- (UIButton *)morebutton {
    if (!_morebutton) {
        _morebutton = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [_morebutton setImage:[UIImage imageNamed:@"MoreDetails_Do"] forState:UIControlStateNormal];
        [_morebutton setImage:[UIImage imageNamed:@"MoreDetails"] forState:UIControlStateSelected];
        [_morebutton addTarget:self action:@selector(moreButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.packView addSubview:_morebutton];
    }
    return _morebutton;
}

- (UIView *)moreView {
    if (!_moreView) {
        _moreView = [[UIView alloc]init];
        _moreView.backgroundColor = [UIColor whiteColor];
        [self.packView addSubview:_moreView];
    }
    return _moreView;
}

- (UIView *)packView {
    if (!_packView) {
        _packView = [[UIView alloc]init];
        _packView.backgroundColor = kSelfViewBackgroundColor;
        [self.contentView addSubview:_packView];
    }
    return _packView;
}

- (MAppointmentHousePopViewController *)mAppointmentHousePopViewController {
    if (!_mAppointmentHousePopViewController) {
        _mAppointmentHousePopViewController = [[MAppointmentHousePopViewController alloc]init];
    }
    return _mAppointmentHousePopViewController;
}

- (ContinuousLandingViewController*)continuousLandingViewController {
    if (!_continuousLandingViewController) {
        _continuousLandingViewController = [[ContinuousLandingViewController alloc]init];
    }
    return _continuousLandingViewController;
}

- (ProjectSigninViewController*)projectSigninViewController {
    if (!_projectSigninViewController) {
        _projectSigninViewController = [[ProjectSigninViewController alloc]init];
    }
    return _projectSigninViewController;
}

- (PartinActivityViewController*)partinActivityViewController {
    if (!_partinActivityViewController) {
        _partinActivityViewController = [[PartinActivityViewController alloc]init];
    }
    return _partinActivityViewController;
}

- (AttentionViewController *)attentionViewController {
    if (!_attentionViewController) {
        _attentionViewController = [[AttentionViewController alloc]init];
    }
    return _attentionViewController;
}

- (UserToAccessViewController*)userToAccessViewController {
    if (!_userToAccessViewController) {
        _userToAccessViewController = [[UserToAccessViewController alloc]init];
    }
    return _userToAccessViewController;
}


- (void)createPopoverController:(UIViewController *)controller {
        _popoverController = [[UIPopoverController alloc] initWithContentViewController:controller];
        _popoverController.delegate = self;

}

-(void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
    
    
}

- (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController {
    return YES;
}


@end
